package Thread.New.线程组;

import Thread.New.线程组.Expection_Deal_With.MyThreadGroup;

import java.util.Random;

public class GroupTest2 {
    public static void main(String[] args) {
        int numberOfThreads = 2*Runtime.getRuntime().availableProcessors();
        MyThreadGroup threadGroup = new MyThreadGroup("MyThreadGroup");
        Task task = new Task();
        for(int i=0; i<numberOfThreads; i++) {
            Thread t = new Thread(threadGroup,task);
            t.start();
        }
        System.out.println("活跃的线程数量为"+threadGroup.activeCount());
        System.out.println("有关线程组的信息");
        threadGroup.list();
        Thread[] threads = new Thread[threadGroup.activeCount()];
        threadGroup.enumerate(threads);
        //将活跃的线程任务放入到这个线程结合中
        for(int i=0; i<threadGroup.activeCount(); i++) {
            System.out.printf("Thread %s: %s\n", threads[i].getName(),threads[i].getState());
        }

    }
}

class Task implements Runnable {

    @Override
    public void run() {
        int result;
        //创建线程任务,用来抛出异常
        Random random = new Random(Thread.currentThread().getId());
        /*while(true) {
            result = 1000/ ((int)(random.nextDouble()*100000000));
            if(Thread.currentThread().isInterrupted()) {
                System.out.printf("%d : Interrupted\n", Thread.currentThread().getId());
            }
        }*/
        int i = 1/0;

    }

}

